import string
import serial
import datetime
import sqlite3

ser = serial.Serial('/dev/ttyUSB0',9600)

con = sqlite3.connect('/home/ben/fridge.db', detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cur = con.cursor()

try:
  while 1:
    line = ser.readline(eol='\n')
    data = string.split(line)
    if len(data) == 3:
      data = [float(i) for i in data]
      time = datetime.datetime.now()
      temp = data[1]
      print time, temp
      cur.execute('insert into data(time, temperature) values (?, ?)', (time, temp))
      con.commit()
except KeyboardInterrupt:
  cur.close()
  con.close()
